package com.example.home;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "home/")
public class HomeController {
    @GetMapping("index")
    public Object home_index(BPage bPage){
        int page_num = bPage.getPage() > 0 ? bPage.getPage() : 1;//StringEmpty.isNULL(page) ? 1 : Integer.parseInt(page);
        int size_num = bPage.getSize() > 0 ? bPage.getSize() : 20;//StringEmpty.isNULL(size) ? 20 : Integer.parseInt(size);
        HashMap<String,Object> map = new HashMap<>();
        String name = "home/index";
        map.put("name",name);
        map.put("phone","15160023363");
        map.put("page",page_num);
        map.put("size",size_num);
        return BRespond.respond_success(map);
    }
    @PostMapping("user")
    public Object home_user(String user_id){
        if (StringEmpty.isNULL(user_id)){
            return BRespond.respond_failure(BEnumRespond.noparam);
        }
        ArrayList<Map<String,Object>> list_data = new ArrayList<>();
        try {
            list_data = BDataBase.search_data_id(user_id);
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
        Object object = list_data.stream().findFirst();
        return BRespond.respond_success(object);
    }
    @PostMapping("search")
    public Object home_search(BPage bPage){
        int page_num = bPage.getPage() > 0 ? bPage.getPage() : 1;
        int size_num = bPage.getSize() > 0 ? bPage.getSize() : 20;
        ArrayList<Map<String,Object>> list_data = new ArrayList<>();
        try {
            list_data = BDataBase.search_data(size_num*(page_num-1),size_num);
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
        return BRespond.respond_success(list_data);
    }
    @PostMapping("delete")
    public Object home_delete(String user_id) {
        if (StringEmpty.isNULL(user_id)){
            BRespond.respond_failure(BEnumRespond.notoken);
        }
        Boolean success = false;
        try {
            return BRespond.respond_success(BDataBase.delete_data(user_id));
        }catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
    }
    @PostMapping(value = "insert")
    public  Object home_insert(BUser user){
        String name = user.getName();
        if (StringEmpty.isNULL(name)){
            return BRespond.respond_failure(BEnumRespond.noparam);
        }
        String phone = user.getPhone();
        int page = user.getAge();
        float height = user.getHeight();
        try {
            boolean success = BDataBase.insert_data(name,page,height,phone);
            return BRespond.respond_success(success);
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
    }
    @PostMapping(value = "update")
    public  Object home_update(BUser user){
        String user_id = user.getUser_id();
        String name = user.getName();
        if (StringEmpty.isNULL(user_id) || StringEmpty.isNULL(name)){
            return BRespond.respond_failure(BEnumRespond.noparam);
        }
        Boolean success = false;
        try {
            return BRespond.respond_success(BDataBase.update_data(user_id,name));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return BRespond.respond_failure_message(205,e.getMessage());
        }
    }
}
